package com.smq.chatserver.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.smq.chatserver.bean.Result;
import com.smq.chatserver.bean.Teem;
import com.smq.chatserver.bean.TeemMember;
import com.smq.chatserver.bean.message.ImMessage;
import com.smq.chatserver.bean.message.Message;
import com.smq.chatserver.bean.message.MessageQueryResult;
import com.smq.chatserver.bean.message.TextMessage;
import com.smq.chatserver.mapper.MessageMapper;
import com.smq.chatserver.mapper.TeemMemberMapper;
import com.smq.chatserver.mapper.TextMessageMapper;
import com.smq.chatserver.mqtt.MqttGateway;
import com.smq.chatserver.services.MessageService;
import com.smq.chatserver.services.TeemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    @Autowired
    TextMessageMapper textMessageMapper;

    @Autowired
    TeemMemberMapper teemMemberMapper;

    @Autowired
    @Qualifier("mqttGateway")
    private MqttGateway mqttGateWay;

    @Autowired
    private TeemService teemService;

    @Override
    public Result<MessageQueryResult> query(String userId, Long time) {
        List<Message> messageList = messageMapper.query(userId, time);
        List<Teem> teems = teemService.query(userId);
        for (Teem teem : teems) {
            messageList.addAll(messageMapper.query(teem.getId(), time));
        }
        List<ImMessage> collect = messageList.stream().map(message -> {
            if (message.getType() == 0) {
                TextMessage textMessage = textMessageMapper.selectById(message.getBodyId());
                return message.textMessageConvert(textMessage);
            }
            return null;
        }).collect(Collectors.toList());

        return Result.success(new MessageQueryResult(collect, collect.size(), false, time));
    }

    public Result<ImMessage> sendText(String msgId, String to, Integer ope, String msg, String userId) {
        TextMessage textMessage = new TextMessage("", 0, msg, 0, null);
        textMessageMapper.insert(textMessage);
        Message message = new Message(msgId, userId, to, ope, 0, textMessage.getId(), System.currentTimeMillis());
        messageMapper.insert(message);
        ImMessage convert = message.textMessageConvert(textMessage);
        if (ope == 0) {
            mqttGateWay.sendToMqtt(new Gson().toJson(convert), to);
        } else {
            List<TeemMember> teemMembers = teemMemberMapper.selectList(new QueryWrapper<TeemMember>().eq("teem_id", to));
            for (TeemMember teemMember : teemMembers) {
                if (!teemMember.getUserId().equals(userId)) {
                    mqttGateWay.sendToMqtt(new Gson().toJson(convert), teemMember.getUserId());
                }
            }
        }
        return Result.success(convert);
    }
}
